package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidVariety;
import com.apache.bid.manager.BidVarietyManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidVarietyManagerImpl implements BidVarietyManager {

	protected IDao bidVarietyDao;
	protected final String entityName = "com.apache.bid.entity.BidVariety";

	public void setBidVarietyDao(IDao bidVarietyDao) {
		this.bidVarietyDao = bidVarietyDao;
	}

	public String saveInfo(ParamsVo<BidVariety> vo) throws BusinessException {
		BidVariety bidVariety = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidVariety_"+infoId;
		bidVariety.setVarietyId(infoId);
		MethodParam param = new MethodParam("BidVariety", cacheKey, "", entityName);
		bidVariety.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		param.setVaule(bidVariety);
		if (bidVarietyDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidVariety> vo) throws BusinessException {
		BidVariety bidVariety = vo.getObj();
		if (Validator.isNotNull(bidVariety.getVarietyId())) {
			String cacheKey = "";
			//String cacheKey="bidVariety_"+bidVariety.getVarietyId();
			MethodParam param = new MethodParam("BidVariety", cacheKey, "", entityName);
			param.setVaule(bidVariety);
			bidVariety.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			return bidVarietyDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidVariety> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidVariety_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidVariety info = (BidVariety) bidVarietyDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidVariety");
			return bidVarietyDao.edit(param);
		} else {
			param.setParams("varietyId", infoId);
			param.setDelete(true);
			return bidVarietyDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidVariety> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidVariety_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidVarietyDao.selectById(param);
	}

	public Object execute(ParamsVo<BidVariety> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidVariety> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by variety_code asc ");
		param.setParams("orderBy", orderBy);
		Page page = bidVarietyDao.pageSelect(param);
		return page;
	}

	public List<BidVariety> getList(ParamsVo<BidVariety> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by variety_code asc ");
		param.setParams("orderBy", orderBy);
		return bidVarietyDao.select(param);
	}

	public long countInfo(ParamsVo<BidVariety> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidVarietyDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidVariety> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidVarietyDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidVariety bidVariety = vo.getObj();
		param.setResultCache(false);
		if (Validator.isNotNull(bidVariety.getVarietyCode())) {
			sb.append(" and varietyCode = :varietyCode");
			param.setParams("varietyCode", bidVariety.getVarietyCode());
		}
		if (Validator.isNotNull(bidVariety.getVarietyName())) {
			sb.append(" and varietyName = :varietyName");
			param.setParams("varietyName", bidVariety.getVarietyName());
		}
		if (Validator.isNotNull(bidVariety.getVarietyStatus())) {
			sb.append(" and varietyStatus = :varietyStatus");
			param.setParams("varietyStatus", bidVariety.getVarietyStatus());
		}
		if (Validator.isNotNull(bidVariety.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidVariety.getCreateTime());
		}
		if (Validator.isNotNull(bidVariety.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidVariety.getModifyTime());
		}
		if (Validator.isNotNull(bidVariety.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidVariety.getSpare1());
		}
		if (Validator.isNotNull(bidVariety.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidVariety.getSpare2());
		}
		if (Validator.isNotNull(bidVariety.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidVariety.getSpare3());
		}
		if (Validator.isNotNull(bidVariety.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidVariety.getSpare4());
		}
		if (Validator.isNotNull(bidVariety.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidVariety.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
